﻿using Interview.API.Admin.CommonEnum;
using Interview.API.Admin.Entities.Rbac;
using Interview.API.Admin.EntityFrameworkCore;
using Interview.API.Admin.RbacDto;
using Interview.API.Admin.ResultDto;
using Interview.API.Admin.ResultDto.Paged;
using Interview.API.Admin.ServiceInterface;
using Interview.API.Admin.ServiceInterface.Rbac;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories;

namespace Interview.API.Admin.RbacService
{
    public class SysMenuService
        : Volo.Abp.Application.Services.ApplicationService, ISysMenuService
    {
        IRepository<SysMenuModel> _repository;
        public SysMenuService(IRepository<SysMenuModel> repository)
        {
            _repository = repository;
        }

        [HttpPost("rbac/menu/delete")]
        public async Task<ServiceResult> DeleteAsync(SysMenuInputDto t)
        {
            ServiceResult _result = new();

            if (t == null || t.Id == Guid.Empty)
            {
                _result.IsFailed(SystemMessageEnum.无效参数.ToString());
            }
            else
            {
                try
                {
                    var model = ObjectMapper.Map<SysMenuInputDto, SysMenuModel>(t);
                    await _repository.DeleteAsync(model);
                    _result.IsSuccess(SystemMessageEnum.操作成功.ToString());
                }
                catch (Exception ex)
                {
                    _result.IsFailed(ex.Message);
                }
            }

            return _result;
        }

        [HttpGet("rbac/menu/all")]
        public async Task<ServiceResult<IEnumerable<SysMenuOutputDto>>> GetAllAsync()
        {
            ServiceResult<IEnumerable<SysMenuOutputDto>> _result = new();

            var allData = await _repository.GetListAsync();
            var mapList = ObjectMapper.Map<IEnumerable<SysMenuModel>, IEnumerable<SysMenuOutputDto>>(allData);
            _result.IsSuccess(mapList, SystemMessageEnum.操作成功.ToString());

            return _result;
        }

        [HttpGet("rbac/menu/all-page")]
        public async Task<ServiceResult<PagedList<SysMenuOutputDto>>> GetAllAsync(int pageIndex = 1, int pageSize = 20)
        {

            ServiceResult<PagedList<SysMenuOutputDto>> _result = new();

            var allData = await _repository.GetListAsync();
            var mapList = ObjectMapper.Map<List<SysMenuModel>, List<SysMenuOutputDto>>(allData);

            PagedList<SysMenuOutputDto> pList = new(mapList.Count(), mapList, pageIndex, pageSize);
            _result.IsSuccess(pList, SystemMessageEnum.操作成功.ToString());


            return _result;
        }

        [HttpGet("rbac/menu/all-page-key")]
        public async Task<ServiceResult<PagedList<SysMenuOutputDto>>> GetAllAsync(
            string key, int pageIndex=1, int pageSize=20)
        {
            ServiceResult<PagedList<SysMenuOutputDto>> _result = new();

            var allData = await _repository.GetListAsync();
            allData = allData.Where(x => x.Name.Contains(key ?? "")).ToList();
            var mapList = ObjectMapper.Map<IEnumerable<SysMenuModel>, IEnumerable<SysMenuOutputDto>>(allData);

            PagedList<SysMenuOutputDto> pList = new(mapList.Count(), mapList, pageIndex, pageSize);
            _result.IsSuccess(pList, SystemMessageEnum.操作成功.ToString());


            return _result;
        }

       
        [HttpGet("rbac/menu/single")]
        public async Task<ServiceResult<SysMenuOutputDto>> GetSingle(string id)
        {
            ServiceResult<SysMenuOutputDto> _result = new();
            if (string.IsNullOrEmpty(id))
            {
                _result.IsFailed(SystemMessageEnum.无效参数.ToString());
            }
            else
            {
                var model = await _repository.GetAsync(x => x.Id.Equals(id));

                var map = ObjectMapper.Map<SysMenuModel, SysMenuOutputDto>(model);
                _result.IsSuccess(map, SystemMessageEnum.操作成功.ToString());
            }
            return _result;
        }

        [HttpPost("rbac/menu/insert")]
        public async Task<ServiceResult<SysMenuOutputDto>> InsertAsync(SysMenuInputDto t)
        {
            #region ABP 使用事务

            //可以先停之前的
            //await CurrentUnitOfWork.SaveChangesAsync();
            //using (var tran = UnitOfWorkManager.Begin(new Volo.Abp.Uow.AbpUnitOfWorkOptions(true), true))
            //{                
            //    await tran.CompleteAsync();
            //}

            #endregion

            ServiceResult<SysMenuOutputDto> _result = new();

            if (t == null)
                _result.IsFailed(SystemMessageEnum.无效参数.ToString());
            else
            {
                try
                {
                    var model = ObjectMapper.Map<SysMenuInputDto, SysMenuModel>(t);
                    await _repository.InsertAsync(model);
                    var dto = ObjectMapper.Map<SysMenuModel, SysMenuOutputDto>(model);
                    _result.IsSuccess(dto, SystemMessageEnum.操作成功.ToString());
                }
                catch (Exception ex)
                {
                    _result.IsFailed(ex.Message);
                }
            }

            return _result;
        }

        [HttpPost("rbac/menu/update")]
        public async Task<ServiceResult> UpdateAsync(SysMenuInputDto t)
        {
            ServiceResult _result = new();

            if (t == null || t.Id == Guid.Empty)
            {
                _result.IsFailed(SystemMessageEnum.无效参数.ToString());
            }
            else
            {
                try
                {
                    var model = ObjectMapper.Map<SysMenuInputDto, SysMenuModel>(t);
                    await _repository.UpdateAsync(model, true);

                    //await CurrentUnitOfWork.SaveChangesAsync();
                    _result.IsSuccess(SystemMessageEnum.操作成功.ToString());

                }
                catch (Exception ex)
                {
                    _result.IsFailed(ex.Message);
                }
            }

            return _result;
        }
    }
}

